package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.DetailActivity;
import flc.ast.adapter.TeachAdapter;
import flc.ast.databinding.FragmentTacticsBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseFragment;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TacticsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TacticsFragment extends BaseNoModelFragment<FragmentTacticsBinding> {

    private TeachAdapter teachAdapter1;
    private TeachAdapter teachAdapter2;
    private int type = 0;
    private int page = 1;
    private int refreshTime = 200;


    @Override
    protected int onCreate() {
        return R.layout.fragment_tactics;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivTacticsType1.setOnClickListener(this);
        mDataBinding.ivTacticsType2.setOnClickListener(this);

        mDataBinding.rvTeachList.setLayoutManager(new LinearLayoutManager(mContext));
        teachAdapter1 = new TeachAdapter();
        mDataBinding.rvTeachList.setAdapter(teachAdapter1);
        teachAdapter1.setOnItemClickListener(this);

        mDataBinding.rvTeachList.setLayoutManager(new LinearLayoutManager(mContext));
        teachAdapter2 = new TeachAdapter();
        teachAdapter2.setOnItemClickListener(this);

        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (type == 0) {
                    getData1();
                } else if (type == 1) {
                    getData2();
                }
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (type == 0) {
                    getData1();
                } else if (type == 1) {
                    getData2();
                }
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivTacticsType1://残局解析
                type = 0;
                mDataBinding.ivTacticsType1.setImageResource(R.drawable.cjjx1);
                mDataBinding.ivTacticsType2.setImageResource(R.drawable.qpdq2);
                mDataBinding.rvTeachList.setAdapter(teachAdapter1);
                break;
            case R.id.ivTacticsType2://棋谱大全
                type = 1;
                mDataBinding.ivTacticsType1.setImageResource(R.drawable.cjjx2);
                mDataBinding.ivTacticsType2.setImageResource(R.drawable.qpdq1);
                mDataBinding.rvTeachList.setAdapter(teachAdapter2);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        DetailActivity.bean = bean;
        startActivity(DetailActivity.class);
    }

    @Override
    protected void initData() {
        getData1();
        getData2();
    }

    private void getData1() {
        StkResApi.getTagResourceList(this, AppConfig.TACTICS_DATA1, StkResApi.createParamMap(0, 10), new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            teachAdapter1.setList(result);
                        } else {
                            teachAdapter1.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getData2() {
        StkResApi.getTagResourceList(this, AppConfig.TACTICS_DATA2, StkResApi.createParamMap(0, 10), new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            teachAdapter2.setList(result);
                        } else {
                            teachAdapter2.addData(result);
                        }
                    }
                }
            }
        });
    }

}